package com.sunny.ihealth.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

/**
 * 网络小工具
 * Created by Sunny  An on 2016/5/15.
 */
public class NetworkUtils {

    /**
     * 是否有网络连接
     *
     * @return
     */
    public static boolean isNetworkAvailable(Context context) {
        if (context != null) {
            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo info = cm.getActiveNetworkInfo();
            if (info != null && info.isConnected()) {
                return true;
            }
        }
        return false;
    }


    /**
     * 是否有网络连接行，可以自动弹出toast提示
     *
     * @param context
     * @param withToast
     * @return
     */
    public static boolean isNetworkAvailable(Context context, boolean withToast) {
        boolean result = isNetworkAvailable(context);
        if (context != null && withToast && !result) {
            Toast.makeText(context, "网络不可用，请检查您的网络设置", Toast.LENGTH_SHORT).show();
        }
        return result;
    }
}
